﻿namespace Arms.Framework.Security.Encryptors
{
    using System;
    using System.Security.Cryptography;
    using System.Text;

    public class SHA256Encryptor : IEncryptor
    {
        public string Encode(string source)
        {
            HashAlgorithm hashAlgorithm = new SHA256CryptoServiceProvider();
            byte[] byteValue = Encoding.UTF8.GetBytes(source);
            byte[] byteHash = hashAlgorithm.ComputeHash(byteValue);
            return Convert.ToBase64String(byteHash);
        }

        public bool IsMatch(string source, string target)
        {
            return source.Equals(this.Encode(target));
        }

        public string CreateSalt(int size = 16)
        {
            using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())
            {
                byte[] buffer = new byte[size];
                rng.GetBytes(buffer);
                return Convert.ToBase64String(buffer);
            }
        }
    }
}